﻿using System.Windows;
using System.Windows.Controls;
using System.Xml;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        public static void RosideMove(Widegets.RowOperationCommand cmd)
        {
            if (Globals.selectedEditor == null) return;
            if (Globals.selectedEditor.SelectedExpression == null) return;

            LunarPresentation.Expressions.Row curRow
                = Globals.selectedEditor.SelectedExpression as Expressions.Row;

            if (curRow == null) return;

            if (curRow.ParentPanel == null) return;

            switch (cmd)
            {
                case Widegets.RowOperationCommand.LeftSideMoveLeft:
                    {
                        if (curRow.ParentPanel.Direction == Orientation.Vertical)
                        {
                            MessageBox.Show("“行”所在的父面板是“垂直”指向的，不能令“行”的左边界向左扩展！",
                                Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }

                        int indexOfCurRow = curRow.ParentPanel.BasePanel.Children.IndexOf(curRow);
                        if (indexOfCurRow <= 0 || indexOfCurRow >= curRow.ParentPanel.BasePanel.Children.Count) return;

                        Expressions.Expression ex = curRow.ParentPanel.BasePanel.Children[indexOfCurRow - 1] as Expressions.Expression;
                        if (ex == null) return;

                        //如果被折叠则是隐藏，此时不允许操作。
                        if (ex.Visibility != Visibility.Visible)
                        {
                            MessageBox.Show("“行”左侧的这个公式被某个标题文本块给折叠起来了，此时不允许扩展！",
                             Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }
                        else
                        {
                            Expressions.BaseText btxt = ex as Expressions.BaseText;
                            if (btxt != null && btxt.IsHeaderText && btxt.IsCollapsed)
                            {
                                MessageBox.Show("“行”左侧的这个公式是一个被折叠的标题文本块，此时不允许扩展！",
                                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                                return;
                            }
                        }

                        //将ex移动到“行”内部。
                        ModifingItem mi = new ModifingItem() { OldSelectedExID = curRow.ExID, NewSelectedExID = curRow.ExID };

                        curRow.ParentPanel.XmlData.RemoveChild(ex.XmlData);

                        Action actDelEx = new Action(ex.ExID, indexOfCurRow - 1, ex.GetType().Name, "DeleteExpression",
                            ex.XmlData.OuterXml, null, ActionType.Delete, curRow.ParentPanel.ExID);
                        curRow.ParentPanel.BasePanel.Children.Remove(ex);

                        mi.Add(actDelEx);

                        curRow.FirstSubPanel.XmlData.InsertBefore(ex.XmlData, curRow.FirstSubPanel.XmlData.FirstChild);

                        Action actInsertEx = new Action(ex.ExID, 0, ex.GetType().Name, "InsertExpression",
                            null, ex.XmlData.OuterXml, ActionType.Insert, curRow.FirstSubPanel.ExID);

                        curRow.FirstSubPanel.BasePanel.Children.Insert(0, ex);
                        ex.ParentPanel = curRow.FirstSubPanel;

                        mi.Add(actInsertEx);
                        curRow.Editor.Do(mi);
                        curRow.Editor.IsModified = true;

                        Expressions.BaseText bt = ex as Expressions.BaseText;
                        if (bt != null && bt.IsHeaderText)
                        {
                            curRow.ParentPanel.RefreshAutoNumbersOfBaseText();
                            curRow.FirstSubPanel.RefreshAutoNumbersOfBaseText();
                        }

                        break;
                    }
                case Widegets.RowOperationCommand.LeftSideMoveRight:
                    {
                        if (curRow.ParentPanel.Direction == Orientation.Vertical)
                        {
                            MessageBox.Show("“行”所在的父面板是“垂直”指向的，不能令“行”的左边界向右收缩！",
                                Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }

                        if (curRow.FirstSubPanel.BasePanel.Children.Count <= 1)
                        {
                            MessageBox.Show("“行”中只剩下一个公式了，不能再收缩边界！",
                                Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }

                        int indexOfCurRow = curRow.ParentPanel.BasePanel.Children.IndexOf(curRow);

                        Expressions.Expression ex = curRow.FirstSubPanel.BasePanel.Children[0] as Expressions.Expression;
                        if (ex == null) return;

                        //如果被折叠则是隐藏，此时不允许操作。
                        if (ex.Visibility != Visibility.Visible)
                        {
                            MessageBox.Show("“行”内部第一个公式被某个标题文本块给折叠起来了，此时不允许收缩！",
                             Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }
                        else
                        {
                            Expressions.BaseText btxt = ex as Expressions.BaseText;
                            if (btxt != null && btxt.IsHeaderText && btxt.IsCollapsed)
                            {
                                MessageBox.Show("“行”内部第一个公式是一个被折叠的标题文本块，此时不允许收缩！",
                                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                                return;
                            }
                        }

                        //将ex移动到“行”的父面板内部。
                        ModifingItem mi = new ModifingItem() { OldSelectedExID = curRow.ExID, NewSelectedExID = curRow.ExID };

                        curRow.FirstSubPanel.XmlData.RemoveChild(ex.XmlData);

                        Action actDelEx = new Action(ex.ExID, 0, ex.GetType().Name, "DeleteExpression",
                            ex.XmlData.OuterXml, null, ActionType.Delete, curRow.FirstSubPanel.ExID);
                        curRow.FirstSubPanel.BasePanel.Children.Remove(ex);

                        mi.Add(actDelEx);

                        curRow.ParentPanel.XmlData.InsertBefore(ex.XmlData, curRow.XmlData);

                        Action actInsertEx = new Action(ex.ExID, indexOfCurRow, ex.GetType().Name, "InsertExpression",
                            null, ex.XmlData.OuterXml, ActionType.Insert, curRow.ParentPanel.ExID);

                        curRow.ParentPanel.BasePanel.Children.Insert(indexOfCurRow, ex);
                        ex.ParentPanel = curRow.ParentPanel;

                        mi.Add(actInsertEx);
                        curRow.Editor.Do(mi);
                        curRow.Editor.IsModified = true;

                        Expressions.BaseText bt = ex as Expressions.BaseText;
                        if (bt != null && bt.IsHeaderText)
                        {
                            curRow.FirstSubPanel.RefreshAutoNumbersOfBaseText();
                            curRow.ParentPanel.RefreshAutoNumbersOfBaseText();
                        }

                        break;
                    }
                case Widegets.RowOperationCommand.TopSideMoveUp:
                    {
                        if (curRow.ParentPanel.Direction == Orientation.Horizontal)
                        {
                            MessageBox.Show("“行”所在的父面板是“水平”指向的，不能令“行”的顶边界向上扩展！",
                                Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }

                        int indexOfCurRow = curRow.ParentPanel.BasePanel.Children.IndexOf(curRow);
                        if (indexOfCurRow <= 0 || indexOfCurRow >= curRow.ParentPanel.BasePanel.Children.Count) return;

                        Expressions.Expression ex = curRow.ParentPanel.BasePanel.Children[indexOfCurRow - 1] as Expressions.Expression;
                        if (ex == null) return;

                        //如果被折叠则是隐藏，此时不允许操作。
                        if (ex.Visibility != Visibility.Visible)
                        {
                            MessageBox.Show("“行”上侧的这个公式被某个标题文本块给折叠起来了，此时不允许扩展！",
                             Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }
                        else
                        {
                            Expressions.BaseText btxt = ex as Expressions.BaseText;
                            if (btxt != null && btxt.IsHeaderText && btxt.IsCollapsed)
                            {
                                MessageBox.Show("“行”上侧的这个公式是一个被折叠的标题文本块，此时不允许扩展！",
                                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                                return;
                            }
                        }

                        //将ex移动到“行”内部。
                        ModifingItem mi = new ModifingItem() { OldSelectedExID = curRow.ExID, NewSelectedExID = curRow.ExID };

                        curRow.ParentPanel.XmlData.RemoveChild(ex.XmlData);

                        Action actDelEx = new Action(ex.ExID, indexOfCurRow - 1, ex.GetType().Name, "DeleteExpression",
                            ex.XmlData.OuterXml, null, ActionType.Delete, curRow.ParentPanel.ExID);
                        curRow.ParentPanel.BasePanel.Children.Remove(ex);

                        mi.Add(actDelEx);

                        curRow.FirstSubPanel.XmlData.InsertBefore(ex.XmlData, curRow.FirstSubPanel.XmlData.FirstChild);

                        Action actInsertEx = new Action(ex.ExID, 0, ex.GetType().Name, "InsertExpression",
                            null, ex.XmlData.OuterXml, ActionType.Insert, curRow.FirstSubPanel.ExID);

                        curRow.FirstSubPanel.BasePanel.Children.Insert(0, ex);
                        ex.ParentPanel = curRow.FirstSubPanel;

                        mi.Add(actInsertEx);
                        curRow.Editor.Do(mi);
                        curRow.Editor.IsModified = true;

                        Expressions.BaseText bt = ex as Expressions.BaseText;
                        if (bt != null && bt.IsHeaderText)
                        {
                            curRow.ParentPanel.RefreshAutoNumbersOfBaseText();
                            curRow.FirstSubPanel.RefreshAutoNumbersOfBaseText();
                        }

                        break;
                    }
                case Widegets.RowOperationCommand.TopSideMoveDown:
                    {
                        if (curRow.ParentPanel.Direction == Orientation.Horizontal)
                        {
                            MessageBox.Show("“行”所在的父面板是“水平”指向的，不能令“行”的顶边界向下收缩！",
                                Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }

                        if (curRow.FirstSubPanel.BasePanel.Children.Count <= 1)
                        {
                            MessageBox.Show("“行”中只剩下一个公式了，不能再收缩边界！",
                                Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }

                        int indexOfCurRow = curRow.ParentPanel.BasePanel.Children.IndexOf(curRow);

                        Expressions.Expression ex = curRow.FirstSubPanel.BasePanel.Children[0] as Expressions.Expression;
                        if (ex == null) return;

                        //如果被折叠则是隐藏，此时不允许操作。
                        if (ex.Visibility != Visibility.Visible)
                        {
                            MessageBox.Show("“行”内部第一个公式被某个标题文本块给折叠起来了，此时不允许收缩！",
                             Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }
                        else
                        {
                            Expressions.BaseText btxt = ex as Expressions.BaseText;
                            if (btxt != null && btxt.IsHeaderText && btxt.IsCollapsed)
                            {
                                MessageBox.Show("“行”内部第一个公式是一个被折叠的标题文本块，此时不允许收缩！",
                                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                                return;
                            }
                        }

                        //将ex移动到“行”的父面板内部。
                        ModifingItem mi = new ModifingItem() { OldSelectedExID = curRow.ExID, NewSelectedExID = curRow.ExID };

                        curRow.FirstSubPanel.XmlData.RemoveChild(ex.XmlData);

                        Action actDelEx = new Action(ex.ExID, 0, ex.GetType().Name, "DeleteExpression",
                            ex.XmlData.OuterXml, null, ActionType.Delete, curRow.FirstSubPanel.ExID);
                        curRow.FirstSubPanel.BasePanel.Children.Remove(ex);

                        mi.Add(actDelEx);

                        curRow.ParentPanel.XmlData.InsertBefore(ex.XmlData, curRow.XmlData);

                        Action actInsertEx = new Action(ex.ExID, indexOfCurRow, ex.GetType().Name, "InsertExpression",
                            null, ex.XmlData.OuterXml, ActionType.Insert, curRow.ParentPanel.ExID);

                        curRow.ParentPanel.BasePanel.Children.Insert(indexOfCurRow, ex);
                        ex.ParentPanel = curRow.ParentPanel;

                        mi.Add(actInsertEx);
                        curRow.Editor.Do(mi);
                        curRow.Editor.IsModified = true;

                        Expressions.BaseText bt = ex as Expressions.BaseText;
                        if (bt != null && bt.IsHeaderText)
                        {
                            curRow.FirstSubPanel.RefreshAutoNumbersOfBaseText();
                            curRow.ParentPanel.RefreshAutoNumbersOfBaseText();
                        }

                        break;
                    }
                case Widegets.RowOperationCommand.RightSideMoveLeft:
                    {
                        if (curRow.ParentPanel.Direction == Orientation.Vertical)
                        {
                            MessageBox.Show("“行”所在的父面板是“垂直”指向的，不能令“行”的右边界向左收缩！",
                                Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }

                        if (curRow.FirstSubPanel.BasePanel.Children.Count <= 1)
                        {
                            MessageBox.Show("“行”中只剩下一个公式了，不能再收缩边界！",
                                Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }

                        int indexOfCurRow = curRow.ParentPanel.BasePanel.Children.IndexOf(curRow);
                        int indexOfLastChild = curRow.FirstSubPanel.BasePanel.Children.Count - 1;

                        Expressions.Expression ex = curRow.FirstSubPanel.BasePanel.Children[indexOfLastChild] as Expressions.Expression;
                        if (ex == null) return;

                        //如果被折叠则是隐藏，此时不允许操作。
                        if (ex.Visibility != Visibility.Visible)
                        {
                            MessageBox.Show("“行”内部最后一个公式被某个标题文本块给折叠起来了，此时不允许收缩！",
                             Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }
                        else
                        {
                            Expressions.BaseText btxt = ex as Expressions.BaseText;
                            if (btxt != null && btxt.IsHeaderText && btxt.IsCollapsed)
                            {
                                MessageBox.Show("“行”内部最后一个公式是一个被折叠的标题文本块，此时不允许收缩！",
                                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                                return;
                            }
                        }

                        //将ex移动到“行”的父面板内部。
                        ModifingItem mi = new ModifingItem() { OldSelectedExID = curRow.ExID, NewSelectedExID = curRow.ExID };

                        curRow.FirstSubPanel.XmlData.RemoveChild(ex.XmlData);

                        Action actDelEx = new Action(ex.ExID, indexOfLastChild, ex.GetType().Name, "DeleteExpression",
                            ex.XmlData.OuterXml, null, ActionType.Delete, curRow.FirstSubPanel.ExID);
                        curRow.FirstSubPanel.BasePanel.Children.Remove(ex);

                        mi.Add(actDelEx);

                        curRow.ParentPanel.XmlData.InsertAfter(ex.XmlData, curRow.XmlData);

                        Action actInsertEx = new Action(ex.ExID, indexOfCurRow + 1, ex.GetType().Name, "InsertExpression",
                            null, ex.XmlData.OuterXml, ActionType.Insert, curRow.ParentPanel.ExID);

                        curRow.ParentPanel.BasePanel.Children.Insert(indexOfCurRow + 1, ex);
                        ex.ParentPanel = curRow.ParentPanel;

                        mi.Add(actInsertEx);
                        curRow.Editor.Do(mi);
                        curRow.Editor.IsModified = true;

                        Expressions.BaseText bt = ex as Expressions.BaseText;
                        if (bt != null && bt.IsHeaderText)
                        {
                            curRow.FirstSubPanel.RefreshAutoNumbersOfBaseText();
                            curRow.ParentPanel.RefreshAutoNumbersOfBaseText();
                        }

                        break;
                    }
                case Widegets.RowOperationCommand.RightSideMoveRight:
                    {
                        if (curRow.ParentPanel.Direction == Orientation.Vertical)
                        {
                            MessageBox.Show("“行”所在的父面板是“垂直”指向的，不能令“行”的右边界向右扩展！",
                                Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }

                        int indexOfCurRow = curRow.ParentPanel.BasePanel.Children.IndexOf(curRow);
                        if (indexOfCurRow < 0 || indexOfCurRow >= curRow.ParentPanel.BasePanel.Children.Count - 1) return;

                        Expressions.Expression ex = curRow.ParentPanel.BasePanel.Children[indexOfCurRow + 1] as Expressions.Expression;
                        if (ex == null) return;

                        //如果被折叠则是隐藏，此时不允许操作。
                        if (ex.Visibility != Visibility.Visible)
                        {
                            MessageBox.Show("“行”右侧的这个公式被某个标题文本块给折叠起来了，此时不允许扩展！",
                                Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }
                        else
                        {
                            Expressions.BaseText btxt = ex as Expressions.BaseText;
                            if (btxt != null && btxt.IsHeaderText && btxt.IsCollapsed)
                            {
                                MessageBox.Show("“行”右侧的这个公式是一个被折叠的标题文本块，此时不允许扩展！",
                                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                                return;
                            }
                        }

                        //将ex移动到“行”内部。
                        ModifingItem mi = new ModifingItem() { OldSelectedExID = curRow.ExID, NewSelectedExID = curRow.ExID };

                        curRow.ParentPanel.XmlData.RemoveChild(ex.XmlData);

                        Action actDelEx = new Action(ex.ExID, indexOfCurRow + 1, ex.GetType().Name, "DeleteExpression",
                            ex.XmlData.OuterXml, null, ActionType.Delete, curRow.ParentPanel.ExID);
                        curRow.ParentPanel.BasePanel.Children.Remove(ex);

                        mi.Add(actDelEx);

                        curRow.FirstSubPanel.XmlData.InsertAfter(ex.XmlData, curRow.FirstSubPanel.XmlData.LastChild);

                        int newIndex = curRow.FirstSubPanel.BasePanel.Children.Count;
                        Action actInsertEx = new Action(ex.ExID,
                            newIndex, ex.GetType().Name, "InsertExpression",
                            null, ex.XmlData.OuterXml, ActionType.Insert, curRow.FirstSubPanel.ExID);

                        curRow.FirstSubPanel.BasePanel.Children.Insert(newIndex, ex);
                        ex.ParentPanel = curRow.FirstSubPanel;

                        mi.Add(actInsertEx);
                        curRow.Editor.Do(mi);
                        curRow.Editor.IsModified = true;

                        Expressions.BaseText bt = ex as Expressions.BaseText;
                        if (bt != null && bt.IsHeaderText)
                        {
                            curRow.ParentPanel.RefreshAutoNumbersOfBaseText();
                            curRow.FirstSubPanel.RefreshAutoNumbersOfBaseText();
                        }

                        break;
                    }
                case Widegets.RowOperationCommand.BottomSideMoveUp:
                    {
                        if (curRow.ParentPanel.Direction == Orientation.Horizontal)
                        {
                            MessageBox.Show("“行”所在的父面板是“水平”指向的，不能令“行”的底边界向上收缩！",
                                Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }

                        if (curRow.FirstSubPanel.BasePanel.Children.Count <= 1)
                        {
                            MessageBox.Show("“行”中只剩下一个公式了，不能再收缩边界！",
                                Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }

                        int indexOfCurRow = curRow.ParentPanel.BasePanel.Children.IndexOf(curRow);
                        int indexOfLastChild = curRow.FirstSubPanel.BasePanel.Children.Count - 1;

                        Expressions.Expression ex = curRow.FirstSubPanel.BasePanel.Children[indexOfLastChild] as Expressions.Expression;
                        if (ex == null) return;

                        //如果被折叠则是隐藏，此时不允许操作。
                        if (ex.Visibility != Visibility.Visible)
                        {
                            MessageBox.Show("“行”内部最后一个公式被某个标题文本块给折叠起来了，此时不允许收缩！",
                             Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }
                        else
                        {
                            Expressions.BaseText btxt = ex as Expressions.BaseText;
                            if (btxt != null && btxt.IsHeaderText && btxt.IsCollapsed)
                            {
                                MessageBox.Show("“行”内部最后一个公式是一个被折叠的标题文本块，此时不允许收缩！",
                                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                                return;
                            }
                        }

                        //将ex移动到“行”的父面板内部。
                        ModifingItem mi = new ModifingItem() { OldSelectedExID = curRow.ExID, NewSelectedExID = curRow.ExID };

                        curRow.FirstSubPanel.XmlData.RemoveChild(ex.XmlData);

                        Action actDelEx = new Action(ex.ExID, indexOfLastChild, ex.GetType().Name, "DeleteExpression",
                            ex.XmlData.OuterXml, null, ActionType.Delete, curRow.FirstSubPanel.ExID);
                        curRow.FirstSubPanel.BasePanel.Children.Remove(ex);

                        mi.Add(actDelEx);

                        curRow.ParentPanel.XmlData.InsertAfter(ex.XmlData, curRow.XmlData);

                        Action actInsertEx = new Action(ex.ExID, indexOfCurRow + 1, ex.GetType().Name, "InsertExpression",
                            null, ex.XmlData.OuterXml, ActionType.Insert, curRow.ParentPanel.ExID);

                        curRow.ParentPanel.BasePanel.Children.Insert(indexOfCurRow + 1, ex);
                        ex.ParentPanel = curRow.ParentPanel;

                        mi.Add(actInsertEx);
                        curRow.Editor.Do(mi);
                        curRow.Editor.IsModified = true;

                        Expressions.BaseText bt = ex as Expressions.BaseText;
                        if (bt != null && bt.IsHeaderText)
                        {
                            curRow.FirstSubPanel.RefreshAutoNumbersOfBaseText();
                            curRow.ParentPanel.RefreshAutoNumbersOfBaseText();
                        }

                        break;
                    }
                case Widegets.RowOperationCommand.BottomSideMoveDown:
                    {
                        if (curRow.ParentPanel.Direction == Orientation.Horizontal)
                        {
                            MessageBox.Show("“行”所在的父面板是“水平”指向的，不能令“行”的底边界向下扩展！",
                                Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }

                        int indexOfCurRow = curRow.ParentPanel.BasePanel.Children.IndexOf(curRow);
                        if (indexOfCurRow < 0 || indexOfCurRow >= curRow.ParentPanel.BasePanel.Children.Count - 1) return;

                        Expressions.Expression ex = curRow.ParentPanel.BasePanel.Children[indexOfCurRow + 1] as Expressions.Expression;
                        if (ex == null) return;

                        //如果被折叠则是隐藏，此时不允许操作。
                        if (ex.Visibility != Visibility.Visible)
                        {
                            MessageBox.Show("“行”下侧的这个公式被某个标题文本块给折叠起来了，此时不允许扩展！",
                                Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }
                        else
                        {
                            Expressions.BaseText btxt = ex as Expressions.BaseText;
                            if (btxt != null && btxt.IsHeaderText && btxt.IsCollapsed)
                            {
                                MessageBox.Show("“行”下侧的这个公式是一个被折叠的标题文本块，此时不允许扩展！",
                                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                                return;
                            }
                        }

                        //将ex移动到“行”内部。
                        ModifingItem mi = new ModifingItem() { OldSelectedExID = curRow.ExID, NewSelectedExID = curRow.ExID };

                        curRow.ParentPanel.XmlData.RemoveChild(ex.XmlData);

                        Action actDelEx = new Action(ex.ExID, indexOfCurRow + 1, ex.GetType().Name, "DeleteExpression",
                            ex.XmlData.OuterXml, null, ActionType.Delete, curRow.ParentPanel.ExID);
                        curRow.ParentPanel.BasePanel.Children.Remove(ex);

                        mi.Add(actDelEx);

                        curRow.FirstSubPanel.XmlData.InsertAfter(ex.XmlData, curRow.FirstSubPanel.XmlData.LastChild);

                        int newIndex = curRow.FirstSubPanel.BasePanel.Children.Count;
                        Action actInsertEx = new Action(ex.ExID,
                            newIndex, ex.GetType().Name, "InsertExpression",
                            null, ex.XmlData.OuterXml, ActionType.Insert, curRow.FirstSubPanel.ExID);

                        curRow.FirstSubPanel.BasePanel.Children.Insert(newIndex, ex);
                        ex.ParentPanel = curRow.FirstSubPanel;

                        mi.Add(actInsertEx);
                        curRow.Editor.Do(mi);
                        curRow.Editor.IsModified = true;

                        Expressions.BaseText bt = ex as Expressions.BaseText;
                        if (bt != null && bt.IsHeaderText)
                        {
                            curRow.ParentPanel.RefreshAutoNumbersOfBaseText();
                            curRow.FirstSubPanel.RefreshAutoNumbersOfBaseText();
                        }

                        break;
                    }
                case Widegets.RowOperationCommand.SplitSubExsToParentPanelOfRow:
                    {
                        //取出curRow中所有的内部公式，并插入到“行”的父面板中。
                        Expressions.SubPanel parentPanel = curRow.ParentPanel;

                        if (parentPanel.BasePanel.Children.Count <= 0) return; //一般应该不可能出现此一情况。

                        int indexOfCurRow = parentPanel.BasePanel.Children.IndexOf(curRow);

                        ModifingItem mi = new ModifingItem() { OldSelectedExID = curRow.ExID };

                        //EndBrace
                        Expressions.Brace endBrace = new Expressions.Brace(curRow.RootPanel, parentPanel, parentPanel.Editor);
                        XmlNode endBraceXmlData = parentPanel.XmlData.InsertXmlAt(Properties.Resources.Brace, indexOfCurRow);
                        endBrace.XmlData = endBraceXmlData;
                        endBrace.Braces = Expressions.Brace.BraceLocation.End;
                        parentPanel.BasePanel.Children.Insert(indexOfCurRow, endBrace);

                        Action actInsertEndBrace = new Action(endBrace.ExID, indexOfCurRow, endBrace.GetType().Name,
                            "InsertExpression", null, endBrace.XmlData.OuterXml, ActionType.Insert, parentPanel.ExID);
                        mi.Add(actInsertEndBrace);
                        //EndBrace_end

                        //插入占位文本块
                        //Row的子面板中不能没有公式。删除Row之前，要保证Row的子面板的后台数据中有一个文本块的Xml节点。
                        //——不然撤销操作会出错！！！
                        XmlNode newBaseTextNode = curRow.FirstSubPanel.XmlData.InsertXmlAt(Properties.Resources.BaseText, 0);
                        Expressions.BaseText newBt = new Expressions.BaseText(curRow.RootPanel,
                            curRow.FirstSubPanel, curRow.Editor);
                        newBt.XmlData = newBaseTextNode;
                        curRow.FirstSubPanel.BasePanel.Children.Insert(0, newBt);

                        Action actInsertNewBaseText = new Action(newBt.ExID, 0, newBt.GetType().Name, "InsertExpression",
                            null, newBt.XmlData.OuterXml, ActionType.Insert, curRow.FirstSubPanel.ExID);
                        mi.Add(actInsertNewBaseText);
                        //插入占位文本块_End

                        for (int i = curRow.FirstSubPanel.BasePanel.Children.Count - 1; i >= 1; i--)
                        {
                            Expressions.Expression ex = curRow.FirstSubPanel.BasePanel.Children[i] as Expressions.Expression;
                            if (ex == null) continue;

                            Action actDelEx = new Action(ex.ExID, i, ex.GetType().Name, "DeleteExpression",
                                ex.XmlData.OuterXml, null, ActionType.Delete, curRow.FirstSubPanel.ExID);

                            curRow.FirstSubPanel.XmlData.RemoveChild(ex.XmlData);
                            curRow.FirstSubPanel.BasePanel.Children.Remove(ex);

                            mi.Add(actDelEx);

                            Action actInsertEx = new Action(ex.ExID, indexOfCurRow, ex.GetType().Name,
                                "InsertExpression", null, ex.XmlData.OuterXml, ActionType.Insert, parentPanel.ExID);

                            parentPanel.XmlData.InsertAt(indexOfCurRow, ex.XmlData);
                            parentPanel.BasePanel.Children.Insert(indexOfCurRow, ex);

                            ex.ParentPanel = parentPanel;

                            mi.Add(actInsertEx);
                        }

                        //StartBrace
                        Expressions.Brace startBrace = new Expressions.Brace(curRow.RootPanel, parentPanel, parentPanel.Editor);
                        XmlNode startBraceXmlData = parentPanel.XmlData.InsertXmlAt(Properties.Resources.Brace, indexOfCurRow);
                        startBrace.XmlData = startBraceXmlData;
                        parentPanel.BasePanel.Children.Insert(indexOfCurRow, startBrace);

                        Action actInsertStartBrace = new Action(startBrace.ExID, indexOfCurRow, startBrace.GetType().Name,
                            "InsertExpression", null, startBrace.XmlData.OuterXml, ActionType.Insert, parentPanel.ExID);
                        mi.Add(actInsertStartBrace);

                        //最后删除Row，注意要用新index
                        Action actDelRow = new Action(curRow.ExID, parentPanel.BasePanel.Children.IndexOf(curRow),
                            curRow.GetType().Name, "DeleteExpression", curRow.XmlData.OuterXml, null,
                            ActionType.Delete, parentPanel.ExID);

                        parentPanel.XmlData.RemoveChild(curRow.XmlData);
                        parentPanel.BasePanel.Children.Remove(curRow);

                        mi.Add(actDelRow);

                        parentPanel.Editor.Do(mi);
                        parentPanel.Editor.IsModified = true;

                        parentPanel.Editor.SelectedExpression = endBrace;
                        mi.NewSelectedExID = endBrace.ExID;

                        break;
                    }
            }
        }
    }
}
